home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / ixemul / sdk / man / cat5 / link.0 < prev    next >
Encoding:
Text File  |  1998-06-15  |  20.4 KB  |  441 lines

  1.  
  2. LINK(5)                    UNIX Programmer's Manual                    LINK(5)
  3.  
  4. NNAAMMEE
  5.      lliinnkk - dynamic loader and link editor interface
  6.  
  7. SSYYNNOOPPSSIISS
  8.      ##iinncclluuddee <<lliinnkk..hh>>
  9.  
  10. DDEESSCCRRIIPPTTIIOONN
  11.      The include file <_l_i_n_k_._h> declares several structures that are present in
  12.      dynamically linked programs and libraries.  The structures define the in-
  13.      terface between several components of the link-editor and loader mecha-
  14.      nism. The layout of a number of these structures within the binaries re-
  15.      sembles the a.out format in many places as it serves such similar func-
  16.      tions as symbol definitions (including the accompanying string table) and
  17.      relocation records needed to resolve references to external entities. It
  18.      also records a number of data structures unique to the dynamic loading
  19.      and linking process. These include references to other objects that are
  20.      required to complete the link-editing process and indirection tables to
  21.      facilitate _P_o_s_i_t_i_o_n _I_n_d_e_p_e_n_d_e_n_t _C_o_d_e (PIC for short) to improve sharing
  22.      of code pages among different processes.  The collection of data struc-
  23.      tures described here will be refered to as the _R_u_n_-_t_i_m_e _R_e_l_o_c_a_t_i_o_n
  24.      _S_e_c_t_i_o_n _(_R_R_S_) and is embedded in the standard text and data segments of
  25.      the dynamically linked program or shared object image as the existing
  26.      a.out format offers no room for it elsewhere.
  27.  
  28.      Several utilities co-operate to ensure that the task of getting a program
  29.      ready to run can complete successfully in a way that optimizes the use of
  30.      system resources. The compiler emits PIC code from which shared libraries
  31.      can be built by ld(1).  The compiler also includes size information of
  32.      any initialized data items through the .size assembler directive. PIC
  33.      code differs from conventional code in that it accesses data variables
  34.      through an indirection table, the Global Offset Table, by convention ac-
  35.      cessable by the reserved name ___G_L_O_B_A_L___O_F_F_S_E_T___T_A_B_L_E__. The exact mechanism
  36.      used for this is machine dependent, usually a machine register is re-
  37.      served for the purpose. The rational behind this construct is to generate
  38.      code that is independent of the actual load address. Only the values con-
  39.      tained in the Global Offset Table may need updating at run-time depending
  40.      on the load addresses of the various shared objects in the address space.
  41.  
  42.      Likewise, procedure calls to globally defined functions are redirected
  43.      through the Procedure Linkage Table (PLT) residing in the data segment of
  44.      the core image. Again, this is done to avoid run-time modifications to
  45.      the text segment.
  46.  
  47.      The linker-editor allocates the Global Offset Table and Procedure Linkage
  48.      Table when combining PIC object files into an image suitable for mapping
  49.      into the process address space. It also collects all symbols that may be
  50.      needed by the run-time link-editor and stores these along with the im-
  51.      age's text and data bits.  Another reserved symbol, ___D_Y_N_A_M_I_C is used to
  52.      indicate the presence of the run-time linker structures. Whenever _DYNAM-
  53.      IC is relocated to 0, there is no need to invoke the run-time link-edi-
  54.      tor. If this symbol is non-zero, it points at a data structure from which
  55.      the location of the necessary relocation- and symbol information can be
  56.      derived. This is most notably used by the start-up module, _c_r_t_0. The _DY-
  57.      NAMIC structure is conventionally located at the start of the data seg-
  58.      ment of the image to which it pertains.
  59.  
  60. DDAATTAA SSTTRRUUCCTTUURREESS
  61.      The data structures supporting dynamic linking and run-time relocation
  62.      reside both in the text and data segments of the image they apply to.
  63.      The text segments contain read-only data such as symbols descriptions and
  64.      names, while the data segments contain the tables that need to be modi-
  65.      fied by during the relocation process.
  66.  
  67.      The _DYNAMIC symbol references a ___d_y_n_a_m_i_c structure:
  68.  
  69.            struct  _dynamic {
  70.                    int     d_version;
  71.                    struct  so_debug *d_debug;
  72.                    union {
  73.                            struct section_dispatch_table *d_sdt;
  74.                    } d_un;
  75.                    struct  ld_entry *d_entry;
  76.            };
  77.  
  78.      _d___v_e_r_s_i_o_n  This field provides for different versions of the dynamic
  79.                 linking implementation. The current version numbers understood
  80.                 by ld and ld.so are _L_D___V_E_R_S_I_O_N___S_U_N _(_3_)_, which is used by the
  81.                 SunOS 4.x releases, and _L_D___V_E_R_S_I_O_N___B_S_D _(_8_)_, which is currently
  82.                 in use by NetBSD.
  83.  
  84.      _d___u_n       Refers to a _d___v_e_r_s_i_o_n dependent data structure.
  85.  
  86.      _d___d_e_b_u_g    this field provides debuggers with a hook to access symbol ta-
  87.                 bles of shared objects loaded as a result of the actions of
  88.                 the run-time link-editor.
  89.  
  90.      _d___e_n_t_r_y    this field is obsoleted by CRT interface version CRT_VER-
  91.                 SION_BSD4, and is replaced by the crt_ldentry in _c_r_t___l_d_s_o.
  92.  
  93.      The _s_e_c_t_i_o_n___d_i_s_p_a_t_c_h___t_a_b_l_e structure is the main ``dispatcher'' table,
  94.      containing offsets into the image's segments where various symbol and re-
  95.      location information is located.
  96.  
  97.            struct section_dispatch_table {
  98.                    struct  so_map *sdt_loaded;
  99.                    long    sdt_sods;
  100.                    long    sdt_paths;
  101.                    long    sdt_got;
  102.                    long    sdt_plt;
  103.                    long    sdt_rel;
  104.                    long    sdt_hash;
  105.                    long    sdt_nzlist;
  106.                    long    sdt_filler2;
  107.                    long    sdt_buckets;
  108.                    long    sdt_strings;
  109.                    long    sdt_str_sz;
  110.                    long    sdt_text_sz;
  111.                    long    sdt_plt_sz;
  112.            };
  113.  
  114.      _s_d_t___l_o_a_d_e_d  A pointer to the first link map loaded (see below). This
  115.                  field is set by ld.so for the benefit of debuggers that may
  116.                  use it to load a shared object's symbol table.
  117.  
  118.      _s_d_t___s_o_d_s    The start of a (linked) list of shared object descriptors
  119.                  needed by _t_h_i_s object.
  120.  
  121.      _s_d_t___p_a_t_h_s   Library search rules. A colon separated list of directories
  122.                  corresponding to the --RR option of ld(1).
  123.  
  124.      _s_d_t___g_o_t     The location of the Global Offset Table within this image.
  125.  
  126.      _s_d_t___p_l_t     The location of the Procedure Linkage Table within this im-
  127.                  age.
  128.  
  129.      _s_d_t___r_e_l     The location of an array of _r_e_l_o_c_a_t_i_o_n___i_n_f_o structures (see
  130.  
  131.  
  132.                  a.out(5)) specifying run-time relocations.
  133.  
  134.      _s_d_t___h_a_s_h    The location of the hash table for fast symbol lookup in this
  135.                  object's symbol table.
  136.  
  137.      _s_d_t___n_z_l_i_s_t  The location of the symbol table.
  138.  
  139.      _s_d_t___f_i_l_l_e_r_2
  140.                  Currently unused.
  141.  
  142.      _s_d_t___b_u_c_k_e_t_s
  143.                  The number of buckets in _s_d_t___h_a_s_h
  144.  
  145.      _s_d_t___s_t_r_i_n_g_s
  146.                  The location of the symbol string table that goes with
  147.                  _s_d_t___n_z_l_i_s_t.
  148.  
  149.      _s_d_t___s_t_r___s_z  The size of the string table.
  150.  
  151.      _s_d_t___t_e_x_t___s_z
  152.                  The size of the object's text segment.
  153.  
  154.      _s_d_t___p_l_t___s_z  The size of the Procedure Linkage Table.
  155.  
  156.      A _s_o_d structure descibes a shared object that is needed to complete the
  157.      link edit process of the object containing it.  A list of such objects
  158.      (chained through _s_o_d___n_e_x_t) is pointed at by the _s_d_t___s_o_d_s in the sec-
  159.      tion_dispatch_table structure.
  160.  
  161.            struct sod {
  162.                    long    sod_name;
  163.                    u_int   sod_library : 1,
  164.                            sod_unused : 31;
  165.                    short   sod_major;
  166.                    short   sod_minor;
  167.                    long    sod_next;
  168.            };
  169.  
  170.      _s_o_d___n_a_m_e     The offset in the text segment of a string describing this
  171.                   link object.
  172.  
  173.      _s_o_d___l_i_b_r_a_r_y  If set, _s_o_d___n_a_m_e specifies a library that is to be searched
  174.                   for by ld.so. The path name is obtained by searching a set
  175.                   of directories (see also ldconfig(8)) for a shared object
  176.                   matching _l_i_b_<_s_o_d___n_a_m_e_>_._s_o_._n_._m. If not set, _s_o_d___n_a_m_e should
  177.                   point at a full path name for the desired shared object.
  178.  
  179.      _s_o_d___m_a_j_o_r    Specifies the major version number of the shared object to
  180.                   load.
  181.  
  182.      _s_o_d___m_i_n_o_r    Specifies the prefered minor version number of the shared
  183.                   object to load.
  184.  
  185.      The run-time link-editor maintains a list of structures called _l_i_n_k _m_a_p_s
  186.      to keep track of all shared objects loaded into a process' address space.
  187.      These structures are only used at run-time and do not occur within the
  188.      text or data segment of an executable or shared library.
  189.  
  190.            struct so_map {
  191.                    caddr_t som_addr;
  192.                    char    *som_path;
  193.                    struct  so_map *som_next;
  194.                    struct  sod *som_sod;
  195.                    caddr_t som_sodbase;
  196.                    u_int   som_write : 1;
  197.                    struct  _dynamic *som_dynamic;
  198.                    caddr_t som_spd;
  199.            };
  200.  
  201.      _s_o_m___a_d_d_r     The address at which the shared object associated with this
  202.                   link map has been loaded.
  203.  
  204.      _s_o_m___p_a_t_h     The full path name of the loaded object.
  205.  
  206.      _s_o_m___n_e_x_t     Pointer to the next link map.
  207.  
  208.      _s_o_m___s_o_d      The _s_o_d structure that was responsible for loading this
  209.                   shared object.
  210.  
  211.      _s_o_m___s_o_d_b_a_s_e  Tossed in later versions the run-time linker.
  212.  
  213.      _s_o_m___w_r_i_t_e    Set if (some portion of) this object's text segment is cur-
  214.                   rently writable.
  215.  
  216.      _s_o_m___d_y_n_a_m_i_c  Pointer to this object's ___d_y_n_a_m_i_c structure.
  217.  
  218.      _s_o_m___s_p_d      Hook for attaching private data maintained by the run-time
  219.                   link-editor.
  220.  
  221.      Symbol description with size. This is simply an _n_l_i_s_t structure with one
  222.      field (_n_z___s_i_z_e) added. Used to convey size information on items in the
  223.      data segment of shared objects. An array of these lives in the shared ob-
  224.      ject's text segment and is addressed by the _s_d_t___n_z_l_i_s_t field of
  225.      _s_e_c_t_i_o_n___d_i_s_p_a_t_c_h___t_a_b_l_e.
  226.  
  227.            struct nzlist {
  228.                    struct nlist    nlist;
  229.                    u_long          nz_size;
  230.            #define nz_un           nlist.n_un
  231.            #define nz_strx         nlist.n_un.n_strx
  232.            #define nz_name         nlist.n_un.n_name
  233.            #define nz_type         nlist.n_type
  234.            #define nz_value        nlist.n_value
  235.            #define nz_desc         nlist.n_desc
  236.            #define nz_other        nlist.n_other
  237.            };
  238.  
  239.      _n_l_i_s_t    (see nlist(5)).
  240.  
  241.      _n_z___s_i_z_e  The size of the data represented by this symbol.
  242.  
  243.      A hash table is included within the text segment of shared object to to
  244.      facilitate quick lookup of symbols during run-time link-editing.  The
  245.      _s_d_t___h_a_s_h field of the _s_e_c_t_i_o_n___d_i_s_p_a_t_c_h___t_a_b_l_e structure points at an array
  246.      of _r_r_s___h_a_s_h structures:
  247.  
  248.            struct rrs_hash {
  249.                    int     rh_symbolnum;           /* symbol number */
  250.                    int     rh_next;                /* next hash entry */
  251.            };
  252.  
  253.      _r_h___s_y_m_b_o_l_n_u_m  The index of the symbol in the shared object's symbol table
  254.                    (as given by the _l_d___s_y_m_b_o_l_s field).
  255.  
  256.      _r_h___n_e_x_t       In case of collisions, this field is the offset of the next
  257.                    entry in this hash table bucket. It is zero for the last
  258.                    bucket element.
  259.      The _r_t___s_y_m_b_o_l structure is used to keep track of run-time allocated com-
  260.      mons and data items copied from shared objects. These items are kept on
  261.      linked list and is exported through the _d_d___c_c field in the _s_o___d_e_b_u_g
  262.      structure (see below) for use by debuggers.
  263.  
  264.            struct rt_symbol {
  265.                    struct nzlist           *rt_sp;
  266.                    struct rt_symbol        *rt_next;
  267.                    struct rt_symbol        *rt_link;
  268.                    caddr_t                 rt_srcaddr;
  269.                    struct so_map           *rt_smp;
  270.            };
  271.  
  272.      _r_t___s_p       The symbol description.
  273.  
  274.      _r_t___n_e_x_t     Virtual address of next rt_symbol.
  275.  
  276.      _r_t___l_i_n_k     Next in hash bucket. Used by internally by ld.so.
  277.  
  278.      _r_t___s_r_c_a_d_d_r  Location of the source of initialized data within a shared
  279.                  object.
  280.  
  281.      _r_t___s_m_p      The shared object which is the original source of the data
  282.                  that this run-time symbol describes.
  283.  
  284.      The _s_o___d_e_b_u_g structure is used by debuggers to gain knowledge of any
  285.      shared objects that have been loaded in the process's address space as a
  286.      result of run-time link-editing. Since the run-time link-editor runs as a
  287.      part of process initialization, a debugger that wishes to access symbols
  288.      from shared objects can only do so after the link-editor has been called
  289.      from crt0.  A dynamically linked binary contains a _s_o___d_e_b_u_g structure
  290.      which can be located by means of the _d___d_e_b_u_g field in ___d_y_n_a_m_i_c.
  291.  
  292.            struct  so_debug {
  293.                    int     dd_version;
  294.                    int     dd_in_debugger;
  295.                    int     dd_sym_loaded;
  296.                    char    *dd_bpt_addr;
  297.                    int     dd_bpt_shadow;
  298.                    struct rt_symbol *dd_cc;
  299.            };
  300.  
  301.      _d_d___v_e_r_s_i_o_n      Version number of this interface.
  302.  
  303.      _d_d___i_n___d_e_b_u_g_g_e_r  Set by the debugger to indicate to the run-time linker
  304.                      that the program is run under control of a debugger.
  305.  
  306.      _d_d___s_y_m___l_o_a_d_e_d   Set by the run-time linker whenever it adds symbols by
  307.                      loading shared objects.
  308.  
  309.      _d_d___b_p_t___a_d_d_r     The address were a breakpoint will be set by the the run-
  310.                      time linker to divert control to the debugger. This ad-
  311.                      dress is determined by the start-up module, _c_r_t_0_._o_, to be
  312.                      some convenient place before the call to _main.
  313.  
  314.      _d_d___b_p_t___s_h_a_d_o_w   Contains the original instruction that was at
  315.                      _d_d___b_p_t___a_d_d_r. The debugger is expected to put this in-
  316.                      struction back before continuing the program.
  317.  
  318.      _d_d___c_c           A pointer to the linked list of run-time allocated sym-
  319.                      bols that the debugger may be interested in.
  320.  
  321.      The _l_d___e_n_t_r_y structure defines a set of service routines within ld.so.
  322.      See dlfcn(3) for more information.
  323.  
  324.            struct ld_entry {
  325.                    void    *(*dlopen)(char *, int);
  326.                    int     (*dlclose)(void *);
  327.                    void    *(*dlsym)(void *, char *);
  328.                    int     (*dlctl)(void *, int, void *);
  329.                    void    (*dlexit) __P((void));
  330.            };
  331.  
  332.      The _c_r_t___l_d_s_o structure defines the interface between ld.so and the start-
  333.      up code in crt0.
  334.  
  335.            struct crt_ldso {
  336.                    int             crt_ba;
  337.                    int             crt_dzfd;
  338.                    int             crt_ldfd;
  339.                    struct _dynamic *crt_dp;
  340.                    char            **crt_ep;
  341.                    caddr_t         crt_bp;
  342.                    char            *crt_prog;
  343.                    char            *crt_ldso;
  344.                    char            *crt_ldentry;
  345.            };
  346.            #define CRT_VERSION_SUN         1
  347.            #define CRT_VERSION_BSD2        2
  348.            #define CRT_VERSION_BSD3        3
  349.            #define CRT_VERSION_BSD4        4
  350.  
  351.      _c_r_t___b_a    The virtual address at which ld.so was loaded by crt0.
  352.  
  353.      _c_r_t___d_z_f_d  On SunOS systems, this field contains an open file descriptor
  354.                to ``/dev/zero'' used to get demand paged zeroed pages. On
  355.                NetBSD systems it contains -1.
  356.  
  357.      _c_r_t___l_d_f_d  Contains an open file descriptor that was used by crt0 to load
  358.                ld.so.
  359.  
  360.      _c_r_t___d_p    A pointer to main's ___d_y_n_a_m_i_c structure.
  361.  
  362.      _c_r_t___e_p    A pointer to the environment strings.
  363.  
  364.      _c_r_t___b_p    The address at which a breakpoint will be placed by the run-
  365.                time linker if the main program is run by a debugger.  See
  366.                _s_o___d_e_b_u_g
  367.  
  368.      _c_r_t___p_r_o_g  The name of the main program as determined by crt0 (CRT_VER-
  369.                SION_BSD3 only).
  370.  
  371.      _c_r_t___l_d_s_o  The path of the run-time linker as mapped by crt0 (CRT_VER-
  372.                SION_BSD4 only).
  373.  
  374.      _c_r_t___l_d_e_n_t_r_y
  375.                The dlfcn(3) entry points provided by the run-time linker
  376.                (CRT_VERSION_BSD4 only).
  377.  
  378.      The _h_i_n_t_s___h_e_a_d_e_r and _h_i_n_t_s___b_u_c_k_e_t structures define the layout of the li-
  379.      brary hints, normally found in ``/var/run/ld.so.hints,'' which is used by
  380.      ld.so to quickly locate the shared object images in the filesystem.  The
  381.      organization of the hints file is not unlike that of an ``a.out'' object
  382.      file, in that it contains a header determining the offset and size of a
  383.      table of fixed sized hash buckets and a common string pool.
  384.  
  385.            struct hints_header {
  386.                    long            hh_magic;
  387.            #define HH_MAGIC        011421044151
  388.                    long            hh_version;
  389.            #define LD_HINTS_VERSION_1      1
  390.                    long            hh_hashtab;
  391.                    long            hh_nbucket;
  392.                    long            hh_strtab;
  393.                    long            hh_strtab_sz;
  394.                    long            hh_ehints;
  395.            };
  396.  
  397.  
  398.      _h_h___m_a_g_i_c      Hints file magic number.
  399.  
  400.      _h_h___v_e_r_s_i_o_n    Interface version number.
  401.  
  402.      _h_h___h_a_s_h_t_a_b    Offset of hash table.
  403.  
  404.      _h_h___s_t_r_t_a_b     Offset of string table.
  405.  
  406.      _h_h___s_t_r_t_a_b___s_z  Size of strings.
  407.  
  408.      _h_h___e_h_i_n_t_s     Maximum usable offset in hints file.
  409.  
  410.            /*
  411.             * Hash table element in hints file.
  412.             */
  413.            struct hints_bucket {
  414.                    int             hi_namex;
  415.                    int             hi_pathx;
  416.                    int             hi_dewey[MAXDEWEY];
  417.                    int             hi_ndewey;
  418.            #define hi_major hi_dewey[0]
  419.            #define hi_minor hi_dewey[1]
  420.                    int             hi_next;
  421.            };
  422.  
  423.      _h_i___n_a_m_e_x   Index of the string identifying the library.
  424.  
  425.      _h_i___p_a_t_h_x   Index of the string representing the full path name of the li-
  426.                 brary.
  427.  
  428.      _h_i___d_e_w_e_y   The version numbers of the shared library.
  429.  
  430.      _h_i___n_d_e_w_e_y  The number of valid entries in _h_i___d_e_w_e_y.
  431.  
  432.      _h_i___n_e_x_t    Next bucket in case of hashing collisions.
  433.  
  434.  
  435. CCAAVVEEAATTSS
  436.      Only the (GNU) C compiler currently supports the creation of shared li-
  437.      braries.  Other programming languages can not be used.
  438.  
  439.  
  440. BSD Experimental               October 23, 1993                              7
  441.